package com.cpi.cornpp.purchase.program.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.cpi.cornpp.biweeks.services.BiWeekService;
import com.cpi.cornpp.biweeks.view.BiWeekView;
import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.costelement.exception.CostElementException;
import com.cpi.cornpp.purchase.program.form.PurchaseProgramQueryForm;
import com.cpi.cornpp.purchase.program.services.PurchaseProgramService;
import com.cpi.cornpp.transports.service.TransportService;

/**
 * Purchase Program List Action
 * @author Edgar Joao
 *
 */
public class PurchaseProgramListAction extends Action{

	/** Default forward*/
	private final static String DEFAULT = "default";
	
	/**
	 * Get A List Of Purchase Program Action
	 * @author Edgar Joao
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int orderField = 0;
		int orderType = 0;
		int biWeekInit = -1;
		int biWeekFinish = -1;
		int transportId = -1;
		PurchaseProgramService purchaseProgramService = null;
		TransportService transportService = null;
		BiWeekService biWeekService = null;
		List purchaseProgramListViews = null;		
		List transportListViews = null;
		List biWeekInitListViews = null;
		List biWeekFinishListViews = null;
		PurchaseProgramQueryForm purchaseProgramQueryForm = null;
		
		HttpSession httpSession = null;
		
		purchaseProgramListViews = new ArrayList(0);
		transportListViews = new ArrayList(0);
		biWeekInitListViews = new ArrayList(0);
		biWeekFinishListViews = new ArrayList(0);
		
		purchaseProgramService = new PurchaseProgramService();
		transportService = new TransportService();
		biWeekService = new BiWeekService();
		
		Calendar calendar = Calendar.getInstance();
		
		if(form != null){
			purchaseProgramQueryForm = (PurchaseProgramQueryForm) form;
			httpSession = request.getSession(false);
			
			if (purchaseProgramQueryForm.getOrderField() != -1){
				orderField = purchaseProgramQueryForm.getOrderField();
			} else if (httpSession.getAttribute("previousOrderField") != null){
				if( purchaseProgramQueryForm.getFilters() == null ){
					orderField = Integer.valueOf((String) httpSession.getAttribute("previousOrderField")).intValue();
				}
			}
			
			if (purchaseProgramQueryForm.getOrderType() != -1){
				orderType = purchaseProgramQueryForm.getOrderType();
			} else if (httpSession.getAttribute("previousOrderType") != null){
				if( purchaseProgramQueryForm.getFilters() == null ){
					orderType = Integer.valueOf((String) httpSession.getAttribute("previousOrderType")).intValue();
				}
			}
			
			if (purchaseProgramQueryForm.getBiWeekInit() != -1){
				biWeekInit = purchaseProgramQueryForm.getBiWeekInit();
			} else if (httpSession.getAttribute("biWeekInit") != null){
				if( purchaseProgramQueryForm.getFilters() == null ){
					biWeekInit = ((Integer) httpSession.getAttribute("biWeekInit")).intValue();
				}
			}
			
			if (purchaseProgramQueryForm.getBiWeekFinish() != -1){
				biWeekFinish = purchaseProgramQueryForm.getBiWeekFinish();
			} else if (httpSession.getAttribute("biWeekFinish") != null){
				if( purchaseProgramQueryForm.getFilters() == null ){
					biWeekFinish = ((Integer) httpSession.getAttribute("biWeekFinish")).intValue();
				}
			}
			
			if (purchaseProgramQueryForm.getTransportIdFilter() >= -1){
				transportId = purchaseProgramQueryForm.getTransportIdFilter();
			} else if (httpSession.getAttribute("transportIdFilter") != null){
				if( purchaseProgramQueryForm.getFilters() == null ){
					transportId = ((Integer) httpSession.getAttribute("transportIdFilter")).intValue();
				}
			}
			
			purchaseProgramQueryForm.setOrderField(orderField);
			purchaseProgramQueryForm.setOrderType(orderType);
			purchaseProgramQueryForm.setBiWeekInit(biWeekInit);
			purchaseProgramQueryForm.setBiWeekFinish(biWeekFinish);
			purchaseProgramQueryForm.setTransportIdFilter(transportId);
			
			transportListViews = transportService.getTransports();
			biWeekInitListViews = biWeekService.getBiWeeks();
			biWeekFinishListViews = biWeekService.getBiWeeks();
			
			request.setAttribute("transportListViews", transportListViews);
			request.setAttribute("biWeekInitListViews", biWeekInitListViews);
			request.setAttribute("biWeekFinishListViews", biWeekFinishListViews);
			
			if( purchaseProgramQueryForm.getBiWeekInit() == -1){
				purchaseProgramQueryForm.setBiWeekInit(getBiweekId(biWeekInitListViews,calendar.get(Calendar.YEAR),1,1));
				purchaseProgramQueryForm.setBiWeekFinish(getBiweekId(biWeekInitListViews,calendar.get(Calendar.YEAR),12,2));
			}
			
			purchaseProgramListViews = purchaseProgramService.list(purchaseProgramQueryForm.getOrderField(),(1 == purchaseProgramQueryForm.getOrderType()) ? false: true, purchaseProgramQueryForm.getBiWeekInit(), purchaseProgramQueryForm.getBiWeekFinish(), purchaseProgramQueryForm.getTransportIdFilter());
			request.setAttribute("purchaseProgramListViews", purchaseProgramListViews);
			
		}
		httpSession.setAttribute("previousOrderField", Integer.toString(purchaseProgramQueryForm.getOrderField()));
		httpSession.setAttribute("previousOrderType", Integer.toString(purchaseProgramQueryForm.getOrderType()));
		httpSession.setAttribute("biWeekInit", new Integer(purchaseProgramQueryForm.getBiWeekInit()));
		httpSession.setAttribute("biWeekFinish", new Integer(purchaseProgramQueryForm.getBiWeekFinish()));
		httpSession.setAttribute("transportIdFilter", new Integer(purchaseProgramQueryForm.getTransportIdFilter()));
		
		return mapping.findForward(DEFAULT);
	}
	
	private int getBiweekId( List biweekViews, int year, int month, int biweek )throws CostElementException{
		int retVal = 0;
		Iterator iterator = null;
		BiWeekView biWeekView = null;
		try{
			iterator = biweekViews.iterator();;
			while( iterator.hasNext() ){
				biWeekView = (BiWeekView)iterator.next();
				if( biWeekView.getYear() == year && biWeekView.getMonth() == month && biWeekView.getBiweek() == biweek ){
					retVal = biWeekView.getBiweekPeriodId();
					break;
				}
			}
		}catch(Exception exception){
			CostElementException costElementException=null;
			exception.printStackTrace(System.out);
			costElementException= new CostElementException(exception , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			throw costElementException;
		}
		return retVal;
	}
}
